<?php
/**
 * @file name : news.php
 * @package   : thokim
 * @author    : Vo Thanh Minh
 * @email     : <thanhminhld@yahoo.com - thanhminhld@gmail.com>
 * @copyright : 2011
 *
 */
if (!defined('BASEPATH')) {
    exit('No direct script access');
}

class News extends Base_Controller
{
    /**
     * __construct
     * @access public
     * @param
     * @return
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
      * _remap
      * @access public
      * @param
      * @return
      */
    public function _remap( $method )
    {
        switch ($method) {

            case 'news-category':
                $this->loadNewsCategory();
                break;

            case 'news-detail':
                $this->loadNewsDetail();
                break;

            case 'real-estate': // bat dong san
                $this->loadRealEstate();
                break;

            case 'real-estate-detail':
                $this->loadRealEstateDetail();
                break;

            default:
                $this->index();
        }
    }
    /**
     * @function    index
     * @access      private
     * @param
     * @return
     */
    private function index()
    {
        $controlName    = $this->uri->segment(1);

        $arrWhere = array('lang_id'   => $this->lang_default,
                          'parent_id' => 0);
        /**
         * Load parent category list
         */
        $modulesList = $this->news_model->loadList($arrWhere, 'all', 0, 'category_id', 'ASC');
        foreach($modulesList as $servCate) {
            $arrWhere = array('lang_id'     => $this->lang_default,
                              'category_id' => $servCate->category_id,
                              'news_show'	=> 'Y');
            $servCate->child = $this->newspost_model->loadList($arrWhere, 6, 0, 'news_id', 'DESC');
        }

        $data = array('urlControlName'  => $controlName . '/',
                      'modulesList'     => $modulesList,
                      'servImgPath'     => 'application/uploads/newspost/',
                      'categoryUrl'     => $controlName . '/news-category/NC-',
                      'postUrl'         => $controlName . '/news-detail/NP-');
        $this->msmarty->assignArrVal($data);

        $main = $this->msmarty->fetch($controlName . '/news.tpl');
        $this->msmarty->assign('main', $main);

        $this->msmarty->view();
    }
    /**
     * @function    loadNewsCategory
     *
     * @author      Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access      private
     *
     */
    private function loadNewsCategory()
    {
        $controlName = $this->uri->segment(1);
        $method      = $this->uri->segment(2);
        $segment3    = $this->uri->segment(3);

        $categoryId  = explode('-', $segment3);
        $categoryId  = $categoryId[1];

        $lastSegment = $this->uri->segment($this->uri->total_segments());
        /**
         * Load news category
         */
        $servCateObj = $this->news_model->loadRecord($categoryId, $this->lang_default);
        if ( ! is_object($servCateObj)) {
            defaultPage($controlName);
        }
        /**
         * Config for paging
         */
        $arrWhere = array('lang_id'     => $this->lang_default,
                          'category_id' => $categoryId,
                          'news_show'	=> 'Y');
        $this->load->library('pagination');
        $config['base_url'] = 'http://' . $_SERVER["HTTP_HOST"] . dirname($_SERVER["SCRIPT_NAME"])
                            . '/' . $controlName . '/news-category/' . $segment3 . '/';
        $totalRows = $this->newspost_model->getTotalRows($arrWhere);
        $config['total_rows'] = $totalRows;
        $config['per_page'] = 10;
        $config['num_links'] = 3;
        $config['page_query_string'] = FALSE;
        $config['last_segment'] = $lastSegment;

        $config['uri_segment'] = 4;
        $config['first_link'] = 'First';
        $config['last_link'] = 'Last';
        $config['next_link'] = 'Next';
        $config['prev_link'] = 'Previous';
        $config['cur_tag_open'] = '<span class="current">';
        $config['cur_tag_close'] = '</span>';

        $this->pagination->initialize($config);
        $str_paging = $this->pagination->create_links();
        $this->msmarty->assign('paging',$str_paging);

        $offset = $this->uri->segment(4);
        if ( ! is_numeric($offset)) {
            $offset = 0;
        }
        /**
         * End paging
         */
        $servPostList = $this->newspost_model->loadList($arrWhere, $config['per_page'], $offset,
                                                        'news_id', 'DESC');
        $data = array('servCateObj'     => $servCateObj,
                      'servPostList'    => $servPostList,
                      'servImgPath'     => 'application/uploads/newspost/',
                      'postUrl'         => $controlName . '/news-detail/NP-',
                      'categoryUrl'     => $controlName . '/news-category/NC-',);
        $this->msmarty->assignArrVal($data);

        $main = $this->msmarty->fetch($controlName . '/news_category.tpl');
        $this->msmarty->assign('main', $main);

        $this->msmarty->view();
    }
    /**
     * @function    loadNewsDetail
     *
     * @author      Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access      private
     */
    private function loadNewsDetail()
    {
        $controlName = $this->uri->segment(1);
        $method      = $this->uri->segment(2);
        $segment3    = $this->uri->segment(3);

        $serviceId  = explode('-', $segment3);
        $serviceId  = $serviceId[1];

        $lastSegment = $this->uri->segment($this->uri->total_segments());

        // Load service detail
        $servpostObj = $this->newspost_model->loadRecord($serviceId, $this->lang_default);
        if ( ! is_object($servpostObj)) {
            defaultPage($controlName);
        }
        // Load service category
        $servCateObj = $this->news_model->loadRecord($servpostObj->category_id, $this->lang_default);
        // Load relation of service detail
        $arrWhere = array('lang_id'     => $this->lang_default,
                          'category_id' => $servpostObj->category_id,
                          'news_id !='  => $serviceId,
                          'news_show'   => 'Y');
        $relationServObj = $this->newspost_model->loadList($arrWhere, 3, 0, 'news_id', 'DESC');

        $data = array('servCateObj'   => $servCateObj,
                      'servpostObj'   => $servpostObj,
                      'otherServPost' => $relationServObj,
                      'servImgPath'   => 'application/uploads/newspost/',
                      'categoryUrl'   => $controlName . '/news-category/NC-',
                      'postUrl'       => $controlName . '/news-detail/NP-');
        $this->msmarty->assignArrVal($data);

        $main = $this->msmarty->fetch($controlName . '/news_detail.tpl');
        $this->msmarty->assign('main', $main);

        $this->msmarty->view();
    }
    /**
     * @function    loadRealEstate
     *
     *
     *
     */
    private function loadRealEstate()
    {
        $controlName = $this->uri->segment(1);
        $method      = $this->uri->segment(2);

        /**
         * Config for paging
         */
        $arrWhere = array('lang_id'   => $this->lang_default,
                          'news_show' => 'Y');
        $this->load->library('pagination');
        $config['base_url'] = 'http://' . $_SERVER["HTTP_HOST"] . dirname($_SERVER["SCRIPT_NAME"])
                            . '/' . $controlName . '/real-estate/';
        $totalRows = $this->newsestate_model->getTotalRows($arrWhere);
        $config['total_rows'] = $totalRows;
        $config['per_page'] = 10;
        $config['num_links'] = 3;
        $config['page_query_string'] = FALSE;
        $config['last_segment'] = '';

        $config['uri_segment'] = 3;
        $config['first_link'] = 'First';
        $config['last_link'] = 'Last';
        $config['next_link'] = 'Next';
        $config['prev_link'] = 'Previous';
        $config['cur_tag_open'] = '<span class="current">';
        $config['cur_tag_close'] = '</span>';

        $this->pagination->initialize($config);
        $str_paging = $this->pagination->create_links();
        $this->msmarty->assign('paging',$str_paging);
        /**
         * End paging
         */
        $offset = $this->uri->segment(3);
        if ( ! is_numeric($offset)) {
            $offset = 0;
        }
        $realEstateNews = $this->newsestate_model->loadList($arrWhere, $config['per_page'],
                                                            $offset, 'news_id', 'DESC');
        $data = array('realEstateNews'  => $realEstateNews,
                      'postUrl'         => 'news/real-estate-detail/ES-');

        $this->msmarty->assignArrVal($data);

        $main = $this->msmarty->fetch($controlName . '/news_estate.tpl');
        $this->msmarty->assign('main', $main);

        $this->msmarty->view();
    }
    /**
     * @function    loadRealEstateDetail
     *
     * @author      Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access      private
     */
    private function loadRealEstateDetail()
    {
        $controlName = $this->uri->segment(1);
        $method      = $this->uri->segment(2);
        $segment3    = $this->uri->segment(3);

        $estateId  = explode('-', $segment3);
        $estateId  = $estateId[1];

        $lastSegment = $this->uri->segment($this->uri->total_segments());
        if ( ! is_numeric($estateId) || $estateId <= 0) {
            defaultPage($controlName);
        }
        // Load news estate detail
        $estateObj = $this->newsestate_model->loadRecord($estateId, $this->lang_default);

        // Load relation of service detail
        $arrWhere = array('lang_id'     => $this->lang_default,
                          'news_id !='  => $estateId,
                          'news_show'   => 'Y');
        /**
         * Config paging
         */
        $this->load->library('pagination');
        $config['base_url'] = 'http://' . $_SERVER["HTTP_HOST"] . dirname($_SERVER["SCRIPT_NAME"])
                            . '/' . $controlName . '/real-estate-detail/' . $segment3 . '/';
        $totalRows = $this->newsestate_model->getTotalRows($arrWhere);
        $config['total_rows'] = $totalRows;
        $config['per_page'] = 10;
        $config['num_links'] = 3;
        $config['page_query_string'] = FALSE;
        $config['last_segment'] = $lastSegment;

        $config['uri_segment'] = 4;
        $config['first_link'] = 'First';
        $config['last_link'] = 'Last';
        $config['next_link'] = 'Next';
        $config['prev_link'] = 'Previous';
        $config['cur_tag_open'] = '<span class="current">';
        $config['cur_tag_close'] = '</span>';

        $this->pagination->initialize($config);
        $str_paging = $this->pagination->create_links();
        $this->msmarty->assign('paging',$str_paging);
        /**
         * End paging
         */
        $offset = $this->uri->segment(4);
        if ( ! is_numeric($offset)) {
            $offset = 0;
        }
        $relationServObj = $this->newsestate_model->loadList($arrWhere, $config['per_page'], $offset,
                                                             'news_id', 'DESC');

        $data = array('estateObj'     => $estateObj,
                      'otherServPost' => $relationServObj,
                      'categoryUrl'   => $controlName . '/real-estate/',
                      'postUrl'       => $controlName . '/real-estate-detail/ES-');
        $this->msmarty->assignArrVal($data);

        $main = $this->msmarty->fetch($controlName . '/news_estate_detail.tpl');
        $this->msmarty->assign('main', $main);

        $this->msmarty->view();
    }
}
?>
